//
// Salvar sempre como UTF-8 pois usamos acentos em algumas mensagens
//
function normalizename(_fd){
	var nome=_fd.value;
	nome=nome.split(" ");
	var retorno="";
	var palavra;
	for (var i=0; i < nome.length; i++) { 
	  palavra = nome[i];
	  palavra=palavra.toLowerCase()+"";
	  if (i>0){
		  retorno+=" ";
	  }
	  if (palavra=="de" || palavra=="da" || palavra=="das" || palavra=="do" || palavra=="dos"){
		  retorno+=palavra;
	  }else{
	  	  letraU=palavra.charAt(0).toUpperCase();
		  palavra=letraU + palavra.substr(1,palavra.length);
		  retorno+=palavra;
	  }
    }
	_fd.value=retorno;
}

function arrayOfDaysInMonths(isLeapYear)

{

   this[0] = 31;

   this[1] = 28;

   if (isLeapYear) this[1] = 29;

   this[2] = 31;

   this[3] = 30;

   this[4] = 31;

   this[5] = 30;

   this[6] = 31;

   this[7] = 31;

   this[8] = 30;

   this[9] = 31;

   this[10] = 30;

   this[11] = 31;

}

function daysInMonth(month, year)

{
 
 

   var isLeapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));

   var monthDays  = new arrayOfDaysInMonths(isLeapYear);

   return monthDays[month];

}

function calendar(_wind,_dia,_mes,_ano)

{
   	_d=_wind.document;
   	var monthNames = "JanFevMarAbrMaiJunJulAgoSetOutNovDez";
	var sinfd=false;
   	var today      = new Date();
	var day;
	var month;
  	var year;
	if (_dia==null || _dia==undefined){
   		day   = today.getDate();
		month = today.getMonth();
	  	year  = today.getYear();
  	}else{
		_dia=eval("document.forms[0]." + _dia);
		var tmp=_dia.value;
		if (_mes==null || _mes==undefined){sinfd = true;}
		if (tmp=="" ){
			day   = today.getDate();
			month = today.getMonth();
			year  = today.getYear();
		}else if (tmp.indexOf("/")!=-1){
			var datas;
			datas=tmp.split('/');
			day        = datas[0];
			month      = datas[1]-1;
			year       = datas[2];
		}else{
			_mes=eval("document.forms[0]." + _mes);
			_ano=eval("document.forms[0]." + _ano);			
			if (_dia.value=="" || _dia.value==null || _dia==undefined){
				day        = today.getDate();
				month      = today.getMonth();
				year       = today.getYear();
			}else{
				day        = _dia.value;
				month      = _mes.value-1;
				year       = _ano.value;
			}
		}
	}
	if ((year*1)<1000){
		year=1900+(year*1);
	}
    _d.write("<scri"+"pt>");
	_d.write("var fd_dia=eval(\"opener.document.forms[0]." + _dia.name + "\");");
	if (!sinfd){
		_d.write("var fd_mes=eval(\"opener.document.forms[0]." + _mes.name + "\");");
		_d.write("var fd_ano=eval(\"opener.document.forms[0]." + _ano.name + "\");");
	}
	_d.write("function dayClick(_dia,_mes,_ano){");
	_d.write("if((_dia*1)<10){_dia=\"0\"+_dia;}");
	_d.write("if((_mes*1)<10){_mes=\"0\"+_mes;}");
	if (sinfd){
		_d.write("fd_dia.value=_dia + \"/\" + _mes + \"/\" + _ano;");
	}else{
		_d.write("fd_dia.value=_dia;");
		_d.write("fd_mes.value=_mes;");
		_d.write("fd_ano.value=_ano;");
	}
	_d.write("top.close();");
	//_d.write("reopen();");
	_d.write("} ");
	_d.write("function reopen(){");
	if (sinfd){
		_d.write("opener.opencalendario(\"" + _dia.name + "\");");
	}else{
		_d.write("opener.opencalendario(\"" + _dia.name + "\",\"" + _mes.name + "\",\"" + _ano.name + "\");");
	}
	_d.write("} ");
	_d.write("function changemes(_mes){");
	if (sinfd){
		_d.write("var tmp=fd_dia.value;");
		_d.write("var datas=tmp.split('/');");
		_d.write("t_day = datas[0];");
		_d.write("t_month = _mes;");
		_d.write("t_year = datas[2];");
		_d.write("if (t_year==undefined){t_year=" + year + ";}");
		_d.write("if (t_day==undefined || t_day==null || t_day==\"\"){t_day=1;}");
		_d.write("if(_mes<1){t_year=(t_year*1)-1;t_month=12;}");
		_d.write("if(_mes>12){t_year=(t_year*1)+1;t_month=1;}");
		_d.write("fd_dia.value=t_day + \"/\" + t_month + \"/\" + t_year;");
	}else{
		_d.write("if(_mes<10){_mes=\"0\"+_mes;}");
		_d.write("fd_mes.value=_mes;");
		_d.write("if(_mes<1){fd_ano.value=(fd_ano.value*1)-1;fd_mes.value=12;}");
		_d.write("if(_mes>11){fd_ano.value=(fd_ano.value*1)+1;fd_mes.value=\"01\";}");
	}
	_d.write("reopen();");
	_d.write("} ");
	_d.write("</scri"+"pt>");
	
   	var numDays    = daysInMonth(month, year);

	var str_data=year+"/"+(month+1)+"/1";
   	var firstDay   = new Date(str_data);
   	var startDay = firstDay.getDay();

   	var column = 0;

   	_d.write("<CENTER>");
   	_d.write("<TABLE BORDER=0 bgcolor=#f8f8f8 Width=\"100%\">");
   	_d.write("<TR><TH><A href=\"javascript:changemes("+ month + ")\">&lt;</A>");
	_d.write("<TH COLSPAN=5><Font face=verdana size=1>");
   	_d.write(monthNames.substring(3*month, 3*(month + 1)) + " " + year);
	_d.write("<TH><A href=\"javascript:changemes("+ (month+2 )+ ")\">&gt;</A>");
   	_d.write("<TR><TH><Font face=verdana size=1>Dom<TH><Font face=verdana size=1>Seg<TH><Font face=verdana size=1>Ter<TH><Font face=verdana size=1>Qua<TH><Font face=verdana size=1>Qui<TH><Font face=verdana size=1>Sex<TH><Font face=verdana size=1>Sab");
   	_d.write("<TR>");

   	for (i=0; i < startDay; i++){
      _d.write("<TD><Font face=verdana size=1>");
      column++;
   	}
   	for (i=1; i <= numDays; i++){
    	var s = "" + i;
      	if (i==day)s = s.fontcolor("#FF0000");
      	s = s.link("javascript:dayClick(\'" + i + "\',\'" + (month+1) + "\',\'" + year + "\')")
        _d.write("<TD align='center'>" + s);
		if (++column == 7){
			_d.write("<TR>");
         	column = 0;
		}
   	}
   _d.write("</TABLE>");
   _d.write("</CENTER>");
   _d.close();
}
 
function opencalendario(dia,mes,ano){
	nj=window.open("about:blank","GPBcalendario","width=300,height=220");
	calendar(nj,dia,mes,ano);
	nj.focus();
}



function validacampo(fd,totchar){
	// fd é o campo
	var mymaxchar;
	if (totchar==null || totchar==undefined){
		mymaxchar=2000;
	}else{
		mymaxchar=totchar;
	}
	
	var txt;
	txt=fd.value;
	if (txt.length>mymaxchar){
		alert("Máximo de caracteres permitidos é " + mymaxchar);
		fd.value=txt.substring(0,mymaxchar);
		fd.focus();
	}
}


function popup(url,W,H,N,R){
	var top;
	var left;
	top=(screen.height/2)-(H/2);
	left=(screen.width/2)-(W/2);
	var wind=window.open(url,N,"left="+ left + ",top="+ top +",width="+ W +",height="+H +",scrollbars=1,resizable=1");
	if (wind==undefined || wind==null){
		alert("Não foi possível abrir o popup.");
	}else{
		wind.focus();
	}
	if (R)
	{
		return wind;
	}
}
function popupMaximized(url,N){
	var wind=window.open(url,N,"resizable=1,toolbar=0,location=0,directories=0,addressbar=0,scrollbars=1,status=0,menubar=0,top=0,left=0,screenX=" + window.screenLeft + ",screenY=" + window.screenTop + ",width=" + (screen.width-8) + ",height=" + (screen.height-60));
	wind.focus();
	return wind;
}
function IsDate(ano_,mes_,dia_) {
	if (ano_!= null && mes_==null && dia_==null){
		arr_=ano_.split("/");
		if (arr_.length!=3){
			return false;
		}
		dia_=arr_[0];
		mes_=arr_[1];
		ano_=arr_[2];
	}
	if (!IsNumericIntPositivo(dia_)){return false;}
	if (!IsNumericIntPositivo(mes_)){return false;}
	if (!IsNumericIntPositivo(ano_)){return false;}
	if (ano_.length!=4){
		return false;
	}

	var f_arrmes=[31,28,31,30,31,30,31,31,30,31,30,31];
	var f_bi=false;
	f_mes_=mes_*1;
	f_dia_=dia_*1;
	f_ano_=ano_*1;
	if (f_ano_<1){return false;}
	if (f_dia_<1){return false;}
	if ((f_ano_/4)==Math.floor(f_ano_/4)){f_bi=true;}
 	if (f_mes_>0 && f_mes_<13){
		if (f_mes_==2 && f_bi){
			if (f_dia_>29){
			return false;}
		}else if(f_dia_>f_arrmes[f_mes_-1]){
			return false;		
		}
	}else{
		return false;
	}
	return true;
}
function IsNumericIntPositivo(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
function IsNumeric(sText)
{
   var ValidChars = "0123456789.-";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
function check_n_fields(nomecampo,tipo,NotNeeded,formulario){
	var fd_;
	contador=1;
	if (formulario==null || formulario==undefined || formulario==""){
		formulario="document.forms[0]";
	}
	fd_=eval(formulario + "." + nomecampo +(contador));
	while (!(fd_ == null || fd_ == undefined)){
		if (fd_.value=="" && (!NotNeeded)){
			alert("Preencha o campo.");
			fd_.focus();
			return false
		}
		if (tipo=="int"){
			if (!IsNumeric(fd_.value)){
				alert("Preencha o campo apenas com números.");
				fd_.focus();
				return false
			}
		}
		if (tipo=="data"){
			if((!NotNeeded) || fd_.value!=""){
				if (!IsDate(fd_.value)){
				alert("Data inválida.");
				fd_.focus();
				return false
				}
			}
		}
		contador+=1;
		fd_=eval(formulario + "." + nomecampo +(contador));
	}
	return true
}
function radiovalue(obj) {
	//validacao de radio buttons sem saber quantos sao
	for (i=0; i<obj.length; i++) {
		if (obj[i].checked) {
			resposta = obj[i].value
			return resposta; 
		}
	}	
	return "";
}
